﻿using System.Collections.Generic;
using Umbraco.Core;
using Umbraco.Core.Persistence;
using UmbracoExtending.Dealer.Model.Config;
using UmbracoExtending.Dealer.Model.Dealer;
using UmbracoExtending.Dealer.Model.District;

namespace UmbracoExtending.Dealer
{
    public class DealerUtil
    {
        /// <summary>
        /// 获取全部省份信息
        /// </summary>
        /// <returns></returns>
        public static List<DistrictSimple> GetProvinces()
        {
            var db = ApplicationContext.Current.DatabaseContext.Database;
            var query = new Sql().Select("ProvinceCode as Code, ProvinceName as Name")
                        .From("[V_umbracoExtendingDealer]")
                        .GroupBy(new string[] { "ProvinceCode", "ProvinceName" })
                        .OrderBy("[Name]");
            return db.Fetch<DistrictSimple>(query);
        }

        /// <summary>
        /// 返回城市信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static List<DistrictSimple> GetCitys(string code)
        {
            var db = ApplicationContext.Current.DatabaseContext.Database;
            var config = db.FirstOrDefault<DealerConfiguration>("SELECT Id, MapPlatform, MapAppKey, ListView, ShowWarning, SimpleMode, ShowTwoLayers FROM [umbracoExtendingDealerConfig]");
            if (config.SimpleMode.Equals("y") && LocalConfiguration.MunicipalityCode.IndexOf(code) >= 0)
            {
                var query1 = new Sql().Select("DistrictCode as Code, DistrictName as Name")
                        .From("[V_umbracoExtendingDealer]")
                        .Where("[ProvinceCode] = @0", code)
                        .GroupBy(new string[] { "DistrictCode", "DistrictName" })
                        .OrderBy("[Name]");
                return db.Fetch<DistrictSimple>(query1);
            }

            var query2 = new Sql().Select("CityCode as Code, CityName as Name")
                        .From("[V_umbracoExtendingDealer]")
                        .Where("[ProvinceCode] = @0", code)
                        .GroupBy(new string[] { "CityCode", "CityName" })
                        .OrderBy("[Name]");
            return db.Fetch<DistrictSimple>(query2);
        }

        /// <summary>
        /// 返回经销商信息
        /// </summary>
        /// <param name="code">编码</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页数字</param>
        /// <returns></returns>
        public static DealerList GetDealers(string code, int pageIndex, int pageSize)
        {
            var db = ApplicationContext.Current.DatabaseContext.Database;
            var config = db.FirstOrDefault<DealerConfiguration>("SELECT Id, MapPlatform, MapAppKey, ListView, ShowWarning, SimpleMode, ShowTwoLayers FROM [umbracoExtendingDealerConfig]");
            var result = new DealerList() { CurrentPage = pageIndex, PageCount = 1, Total = 1, Dealers = new List<DealerItem>() };
            
            string ColumnName = (config.SimpleMode.Equals("y") ? "DistrictCode" : "CityCode");

            var q1 = new Sql().Select("COUNT(*)").From("[V_umbracoExtendingDealer]").Where(ColumnName + " = @0", code);
            result.Total = db.ExecuteScalar<int>(q1);

            if ((result.Total % pageSize) > 0)
            {
                result.PageCount = (result.Total - result.Total % pageSize) / pageSize + 1;
            }
            else
            {
                result.PageCount = result.Total / pageSize;
            }

            var query = new Sql().Select("Id, Province, City, District, Address, StoreName, ResponsiblePerson, ContactNumber, Lat, Lng, IsMistake")
                .From("[V_umbracoExtendingDealer]")
                .Where(ColumnName + " = @0", code);
            var dealers = db.Page<DealerItem>(pageIndex, pageSize, query.OrderByDescending("Id")).Items;
            result.Dealers = dealers;

            return result;
        }
    }
}
